﻿//创建时间: 2010-12-14
//创建作者: 李晓忠
//功能说明: 广告列表
//最后修改: 2010-12-15 李晓忠 添加获取最大ID,通过
using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;
using System.Data;
using TRPOP.Common;
using TRPOP.Config;
using TRPOP.CMS.Model;
using TRPOP.CMS.DALFactory;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace TRPOP.CMS.DALSQLServer
{
    public class AdvscontentDAL : IAdvscontentDAL
    {

        #region 保存 Save
        public EnumSubmitResult Save(AdvscontentInfo info)
        {
            string strStoreProcedure = SystemInfoHelper.getSystemInfo().DBprefixTableName + "_" + "advscontent_Save";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);

            odbDataBase.AddInParameter(odbCommand, "@CustomersID", System.Data.DbType.Int32, info.CustomersID);
            odbDataBase.AddInParameter(odbCommand, "@Name", System.Data.DbType.String, info.Name);
            odbDataBase.AddInParameter(odbCommand, "@ShowModel", System.Data.DbType.Int32, info.ShowModel);
            odbDataBase.AddInParameter(odbCommand, "@AddTime", System.Data.DbType.DateTime, info.AddTime);
            odbDataBase.AddInParameter(odbCommand, "@AddUserId", System.Data.DbType.Int32, info.AddUserId);
            odbDataBase.AddInParameter(odbCommand, "@UpdateTime", System.Data.DbType.DateTime, info.UpdateTime);
            odbDataBase.AddInParameter(odbCommand, "@UpdateUserId", System.Data.DbType.Int32, info.UpdateUserId);
            odbDataBase.AddInParameter(odbCommand, "@IsValid", System.Data.DbType.Boolean, info.IsValid);
            odbDataBase.AddInParameter(odbCommand, "@MaturityTime", System.Data.DbType.DateTime, info.MaturityTime);
            odbDataBase.AddInParameter(odbCommand, "@Hits", System.Data.DbType.Int32, info.Hits);
            odbDataBase.AddInParameter(odbCommand, "@Detial", System.Data.DbType.String, info.Detial);
            odbDataBase.AddInParameter(odbCommand, "@PageName", System.Data.DbType.String, info.PageName);
           // odbDataBase.AddInParameter(odbCommand, "@Description", System.Data.DbType.String, info.Description);
            odbDataBase.AddInParameter(odbCommand, "@Redirect", System.Data.DbType.String, info.Redirect);
            odbDataBase.AddParameter(odbCommand, "@AdID", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, info.AdID);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                info.AdID = (int)odbDataBase.GetParameterValue(odbCommand, "@AdID");
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }

        #endregion

        #region 获取实体 GetInfoById
        public AdvscontentInfo GetInfoById(int id)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_advs_advscontent WHERE AdID=" + id;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            AdvscontentInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new AdvscontentInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }


        public AdvscontentInfo GetInfoByPageName(string pageName)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_advs_advscontent WHERE PageName='"+pageName+"'";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            AdvscontentInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new AdvscontentInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }
        #endregion

        #region 删除 DeleteInfoById
        public EnumSubmitResult DeleteInfoById(int id)
        {
            return DeleteInfoByIds(id.ToString());
        }
        #endregion

        #region 删除 DeleteInfoByIds
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            string strSqlString = "DELETE FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_advs_advscontent WHERE AdID in(" + ids + ")";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }
        #endregion

        #region 自定义列表（无分页） GetListByCondition
        public List<AdvscontentInfo> GetListByCondition(int topn, string condition, string orderby)
        {
            string strSqlString = "SELECT ";
            if (topn > 0)
            {
                strSqlString += " TOP " + topn;
            }
            strSqlString += " * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_advs_advscontent WHERE 1=1"; if (condition != "")
            {
                strSqlString += condition;
            }
            if (orderby != "")
            {
                strSqlString += " ORDER BY " + orderby;
            }
            else
            {
                strSqlString += " ORDER BY AdID DESC";
            }

            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            List<AdvscontentInfo> olstInfo = new List<AdvscontentInfo>(); AdvscontentInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new AdvscontentInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion

        #region 自定义列表（分页） GetListByPager
        public List<AdvscontentInfo> GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            string strStoreProcedure = SystemInfoHelper.getSystemInfo().PagerStoreProcedureName;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);
            List<AdvscontentInfo> olstInfo = new List<AdvscontentInfo>(); AdvscontentInfo oInfo = null;
            odbDataBase.AddInParameter(odbCommand, "@TableName", System.Data.DbType.String, SystemInfoHelper.getSystemInfo().DBprefixTableName + "_advs_advscontent");
            odbDataBase.AddInParameter(odbCommand, "@ShowColumns", System.Data.DbType.String, "*");
            odbDataBase.AddInParameter(odbCommand, "@pageNumber", System.Data.DbType.Int32, pageNumber);
            odbDataBase.AddInParameter(odbCommand, "@pageSize", System.Data.DbType.Int32, pageSize);
            odbDataBase.AddInParameter(odbCommand, "@condition", System.Data.DbType.String, condition);
            odbDataBase.AddInParameter(odbCommand, "@orderby", System.Data.DbType.String, orderby);
            odbDataBase.AddOutParameter(odbCommand, "@recordCount", System.Data.DbType.Int32, 0);
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new AdvscontentInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                }
                recordCount = int.Parse(odbCommand.Parameters["@RecordCount"].Value.ToString());
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion


        #region 获取最大Id
        /// <summary>
        /// 获取最大Id
        /// </summary>
        /// <returns></returns>
        public int GetMaxId()
        {
            int intMaxId = 0;
            string strSqlString = "SELECT ISNULL( Max(AdID),0)   AS 'AdID'  FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_advs_advscontent ";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            AdvscontentInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new AdvscontentInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            if (oInfo != null)
            {
                intMaxId = oInfo.AdID;
            }
            return intMaxId;
        }
        #endregion


    }
}
